#include <sdk.h>
#include <wx/aui/aui.h>
#include "FileManager.h"

namespace {
  PluginRegistrant<FileManagerPlugin> reg( _T( "FileManager" ) );
}

int ID_ProjectOpenInFileBrowser = wxNewId();

BEGIN_EVENT_TABLE( FileManagerPlugin, M_Code_Plugin )
  EVT_MENU( ID_ProjectOpenInFileBrowser, FileManagerPlugin::OnOpenProjectInFileBrowser )
END_EVENT_TABLE()

FileManagerPlugin::FileManagerPlugin() {
  if( !Manager::LoadResource( _T( "FileManager.zip" ) ) ) {
    NotifyMissingFile( _T( "FileManager.zip" ) );
  }
  m_fe = 0;
}

FileManagerPlugin::~FileManagerPlugin() {
}

void FileManagerPlugin::OnAttach() {
  m_fe = new FileExplorer( Manager::Get()->GetAppWindow() );
  Manager::Get()->GetProjectManager()->GetUI().GetNotebook()->AddPage( m_fe, _( "文件管理器" ) );
}

void FileManagerPlugin::OnRelease( bool ) {
  if( m_fe ) {
    M_Code_AuiNotebook *notebook = Manager::Get()->GetProjectManager()->GetUI().GetNotebook();
    int idx = notebook->GetPageIndex( m_fe );
    if( idx != -1 ) {
      notebook->RemovePage( idx );
    }
    delete m_fe;
  }
  m_fe = 0;
}

void FileManagerPlugin::BuildModuleMenu( const ModuleType type, wxMenu* menu, const FileTreeData* data ) {
  if( type == mtProjectManager && data && data->GetKind() == FileTreeData::ftdkProject ) {
    m_project_selected = wxFileName( data->GetProject()->GetFilename() ).GetPath();
    menu->Append( ID_ProjectOpenInFileBrowser, _( "文件管理器" ) );
  }
}

void FileManagerPlugin::OnOpenProjectInFileBrowser( wxCommandEvent& ) {
  M_Code_AuiNotebook *m_nb = Manager::Get()->GetProjectManager()->GetUI().GetNotebook();
  m_nb->SetSelection( m_nb->GetPageIndex( m_fe ) );
  m_fe->SetRootFolder( m_project_selected );
}
